/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.panomedic.core;

import com.panomedic.log4j.LogCategories;
import com.panomedic.log4j.Logger;
import java.util.Arrays;

/**
 *
 * @author Yare
 */
public class myRaster {

    static Logger logger = new Logger();
    double[][][] raster = null;
    int width;
    int height;
    private double[] mean = null;
    private double[] sd = null;

    public double[] getMean() {
        return mean;
    }

    public double[] getSD() {
        return sd;
    }

    public myRaster(int w, int h) {

        raster = new double[w][h][4];

        this.width = w;
        this.height = h;

    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    double[] getPixel(int x, int y, double[] px) {
        if (x < 0 || y < 0 || x >= width || y >= height) {
            return null;
        }
        return raster[x][y];
    }

    public void setPixel(int x, int y, double[] px) {
        if (x < 0 || y < 0 || x >= width || y >= height) {
            return;
        }
        if (px == null) {
            return;
        }
        raster[x][y] = px;
    }

    private double[] countMean() {
        double sum[] = new double[3];
        double count = 0;
        double px[] = null;
        for (int x = 0; x < getWidth(); x++) {
            for (int y = 0; y < getHeight(); y++) {
                px = getPixel(x, y, px);
                if (px[3] == 255) {
                    for (int i = 0; i < 3; i++) {
                        sum[i] += px[i];
                    }
                    count++;
                }
            }
        }

        for (int i = 0; i < 3; i++) {
            if (count != 0)
                sum[i] /= count;
            else 
                sum[i] = Double.MAX_VALUE;
        }
        return sum;
    }

    private double[] countSD(double[] mean) {

        double sum[] = new double[3];
        double count = 0;
        double px[] = null;
        for (int x = 0; x < getWidth(); x++) {
            for (int y = 0; y < getHeight(); y++) {
                px = getPixel(x, y, px);
                if (px[3] == 255) {
                    for (int i = 0; i < 3; i++) {
                        double t = px[i] - mean[i];
                        sum[i] += Math.pow(t, 2);
                    }
                    count++;
                }
            }
        }

        for (int i = 0; i < 3; i++) {
            sum[i] /= count - 1;
            sum[i] = Math.sqrt(sum[i]);
        }
        return sum;
    }

    public void count() {
        logger.info(LogCategories.PROCESSING, "Counting ADJUSTED stats ");
//        double[] gamma = new double[]{0,0,0};
        mean = countMean();
        sd = countSD(mean);

        logger.debug(LogCategories.PROCESSING, "mean=" + Arrays.toString(getMean()));
        logger.debug(LogCategories.PROCESSING, "std dev=" + Arrays.toString(getSD()));
        logger.info(LogCategories.PROCESSING, "Done.");
    }
}